package com.lzdongrui.backend.quartz;

import com.lzdongrui.backend.domain.ApplyLeave;
import com.lzdongrui.backend.domain.vo.ApplyLeaveVO;
import com.lzdongrui.backend.mapper.ApplyLeaveMapper;
import com.lzdongrui.backend.mapper.StudentinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * 假期状态定时器
 *
 * @author hjs
 * @date 2023-06-18
 */
@Component("applyLeaveTimer")
public class ApplyLeaveTimer {
    @Autowired
    private ApplyLeaveMapper applyLeaveMapper;
    @Autowired
    private StudentinfoMapper studentinfoMapper;

    public void applyLeaveTimer() {
        System.out.println("执行一次假期状态管理");

        List<ApplyLeave> applyLeaveS = applyLeaveMapper.selApplyLeaveTimerList();// 获取全部已通过审核且未开始与进行中的列表
        if (applyLeaveS.size() > 0){
            Long newDate = new Date().getTime();// 获取当前时间
            for (ApplyLeave applyLeave : applyLeaveS) {
                if (applyLeave.getBeginTime().getTime() <= newDate && newDate <= applyLeave.getEndTime().getTime()) {// 如果当前时间在请假时间范围内
                    applyLeave.setVacationStatus(2);// 设置请假状态为进行中
                    applyLeaveMapper.updateApplyLeave(applyLeave);// 更新请假状态
                    studentinfoMapper.upStuIsAtSchoolByStudentId(applyLeave.getStu(), 1);// 更新学生在校状态为不在校
                }
                if (applyLeave.getEndTime().getTime() < newDate) {// 如果当前时间在请假时间范围之后
                    applyLeave.setVacationStatus(3);// 设置请假状态为已失效
                    applyLeaveMapper.updateApplyLeave(applyLeave);// 更新请假状态
                }
            }
        }
    }
}
